package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 破晓属性匹配
 * @author ruoyi
 * @data 2024/7/5
 * @apiNote
 */
@Service
public class MAIGEHAOWANAttr implements AdapterAttr {
    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> resultList = new ArrayList<>();
        String allGame = getAllGame();
        List<JSONObject> array = JSONObject.parseArray(JSONObject.parseObject(allGame).getString("data"), JSONObject.class);
        for (JSONObject jsonObject : array) {
            GamesRes res = new GamesRes();
            res.setId(jsonObject.getString("game_id"));
            res.setGameName(jsonObject.getString("game_id_name"));
            resultList.add(res);
        }
        return resultList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {

        return new ArrayList<>();
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();

        String response = getAtta(gameId);
        JSONObject jsonObject = JSONObject.parseObject(response);
        String property = jsonObject.getString("data");
        List<JSONObject> data = JSONArray.parseArray(property, JSONObject.class);
        for(JSONObject entry : data){
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(entry.getString("id"));
            interAttrRes.setName(entry.getString("brandname"));
//            List<JSONObject> jsonObjects = entry.getValue();
//            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
//            for (JSONObject jsonObject1 : jsonObjects) {
//                InterAttrRes interAttrRes1 = new InterAttrRes();
//                String skin_id = jsonObject1.getString("skin_id");
//                interAttrRes1.setId(skin_id);
//                String cate_name = jsonObject1.getString("cate_name");
//                interAttrRes1.setName(cate_name);
//                interAttrResList1.add(interAttrRes1);
//            }
//            interAttrRes.setInterServerRes(interAttrResList1);
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }

    public static void main(String[] args) {
        getAtta("10");
//        getAllGame();
//        getAllServer("11");
    }

    static String getAllServer(String gameId) {
        String url = "http://www.maigehaowan.com/api/api.php?pg=getAllServer";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getAllServer");
        params.put("gameId", gameId);
        String str = getCover1(params) + "key=" + "x8logf11d6o898l83botgg105c9ky83rjoi";
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    static String getAtta(String gameId) {
        String url = "http://www.maigehaowan.com/api/api.php?pg=getGameId";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getGameId");
        params.put("gameId", gameId);
        String str = getCover1(params) + "key=" + "h47p1l4c59o05ynzx9xs04ss8e08xp3e99p";
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    static String getAllGame() {
        String url = "http://www.maigehaowan.com/api/api.php?pg=getAllGame";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getAllGame");
        String str = getCover1(params) + "key=" + "h47p1l4c59o05ynzx9xs04ss8e08xp3e99p";
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    public static String getCover1(Map<String, Object> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            resut.append(key + "=" + params.get(key) + "&");
        }
        return resut.toString();
    }
}
